package com.blood.core.resp;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/13 11:35
 * @description 用于系统之间传输的响应结果对象
 */
@Data
@ApiModel(value = "请求响应消息体", description = "请求响应消息体")
public final class ResponseResult<T> {

    /**
     * 操作结果的成功标识
     */
    @ApiModelProperty("操作结果的成功标识")
    private boolean success;
    /**
     * 响应时间戳
     */
    @ApiModelProperty("响应时间戳")
    private String timestamp;
    /**
     * 响应状态码
     */
    @ApiModelProperty("响应状态码")
    private String code;
    /**
     * 响应消息
     */
    @ApiModelProperty("响应消息")
    private String message;
    /**
     * 响应数据
     */
    @ApiModelProperty("响应数据")
    private T body;

    public boolean fail() {
        return !success;
    }

    public ResponseResult() {
    }

    public ResponseResult(boolean success, String reCode, String reMsg, T body) {
        this.success = success;
        this.code = reCode;
        this.message = reMsg;
        this.body = body;
        this.timestamp = DateUtil.now();
    }

    public static <T> ResponseResult<T> createFrom(HandleResult result) {
        return createFrom(result, null);
    }

    public static <T> ResponseResult<T> createFrom(HandleResult result, T body) {
        return new ResponseResult<>(result.isSuccess(), result.getCode(), result.getMessage(), body);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }
}
